
import {defineStore} from 'pinia'

export type Tab = {
    title:string;
    path:string;
}
export type TabState = {
    tabList:Tab[]
}
export const tabStore = defineStore('tabStore',{
    state:()=>{
        return {
            tabList: [] as Tab[]
        }
    },
    getters:{
        getTab(state){
            return state.tabList
        }
    },
    actions:{
        addTab(tab:Tab){
            //判断数据是否在选项卡里面
            if(this.tabList.some(item=>item.path === tab.path)) return;
            if(tab.path == '/dashboard'){
                //如果是首页，加到第一个
                this.tabList.unshift(tab)
            }else{
                this.tabList.push(tab)
            }
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage, paths: ['tabList'] }
        ]
    }
})